package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import entities.Appointment;
import exceptions.IllegalAppointmentException;
import exceptions.KeinFreierTermin;
import exceptions.WrongDateException;
import fachlogik.Administration;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class RescheduleWindow extends JFrame {

private JPanel               contentPane;
private JTextField           tf_von;
private JTextField           tf_bis;
private SimpleDateFormat     sf;
private List<Appointment>    appointmentsToReschedule;
private DefaultComboBoxModel dcm;
private Administration       instance;

/**
 * Launch the application.
 */
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// RescheduleWindow frame = new RescheduleWindow();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }

/**
 * Create the frame.
 */
public RescheduleWindow(Administration a, List<Appointment> list) {
  instance = a;
  appointmentsToReschedule = list;
  sf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
  sf.setLenient(false);
  dcm = new DefaultComboBoxModel<>();
  setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  setBounds(100, 100, 449, 173);
  setLocationRelativeTo(null);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  contentPane.setLayout(new BorderLayout(0, 0));
  setContentPane(contentPane);
  setResizable(false);

  JPanel panel = new JPanel();
  contentPane.add(panel, BorderLayout.CENTER);
  panel.setLayout(null);

  JLabel lblReschedule = new JLabel("Reschedule");
  lblReschedule.setBounds(10, 11, 95, 14);
  panel.add(lblReschedule);

  JLabel lblVon = new JLabel("From:");
  lblVon.setBounds(20, 36, 46, 14);
  panel.add(lblVon);

  tf_von = new JTextField();
  tf_von.setBounds(60, 36, 104, 20);
  panel.add(tf_von);
  tf_von.setColumns(10);

  tf_von.setText(sf.format(new Date()));

  JLabel lblBis = new JLabel("To:");
  lblBis.setBounds(184, 36, 46, 14);
  panel.add(lblBis);

  tf_bis = new JTextField();
  tf_bis.setBounds(236, 36, 112, 20);
  panel.add(tf_bis);
  tf_bis.setColumns(10);

  dcm.addElement("Most important appointment first");
  dcm.addElement("Shortest appointment first");
  final JComboBox comboBox = new JComboBox(dcm);
  comboBox.setBounds(20, 85, 203, 20);
  panel.add(comboBox);

  JButton btnReschedule = new JButton("Reschedule");
  btnReschedule.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      try {
        /*
         * if (isThisDateValid(tf_von.getText()) &&
         * isThisDateValid(tf_bis.getText())) { throw new
         * WrongDateException(); }
         */
        
        Date von = sf.parse(tf_von.getText());
        Date bis = sf.parse(tf_bis.getText());
        if (von.before(bis)) {
          Date today = new Date();
          if (von.after(today) && bis.after(today)) {
            instance.setRescheduleErlaubtVon(von);
            instance.setRescheduleErlaubtBis(bis);

            /*
             * if (!von.toString().matches(
             * "([0-2]?[0-9]|30|31)\\.([0-9]|10|11|12)\\.[0-9][0-9][0-9][0-9\\s[0-1]?[0-9]|2[0-3]):[0-5][0-9])"
             * ) || !bis.toString().matches(
             * "([0-2]?[0-9]|30|31)\\.([0-9]|10|11|12)\\.[0-9][0-9][0-9][0-9\\s[0-1]?[0-9]|2[0-3]):[0-5][0-9])"
             * )){ throw new WrongDateException(); }
             */
            System.out.println(sf.format(von));
            System.out.println(sf.format(bis));

            String algorithm = (String) comboBox
                .getSelectedItem();
            if (algorithm
                .equals("Most important appointment first"))
              instance.mostImportantAppointment(von, bis,
                  appointmentsToReschedule);
            if (algorithm.equals("Shortest appointment first"))
              instance.shortestAppointmentFirst(von, bis,
                  appointmentsToReschedule);
          }
        } else
          JOptionPane.showMessageDialog(null,
              "Ende liegt vor Beginn!");
      } catch (ParseException e1) {
        JOptionPane
            .showMessageDialog(
                null,
                "Please enter dates thusly: dd.MM.yyyy HH:mm!");
      } catch (IllegalAppointmentException e1) {
        JOptionPane.showMessageDialog(null,
            "Something went wrong, Jim!",
            "Rescheduling failed!", 1);
        e1.printStackTrace();
      } catch (KeinFreierTermin e1) {
        JOptionPane.showMessageDialog(null, e1.getMessage());
    }
    }
  });
  btnReschedule.setBounds(289, 85, 89, 23);
  panel.add(btnReschedule);
}

public boolean isThisDateValid(String dateToValidate) {

  if (dateToValidate == null) {
    return false;
  }

  SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
  sdf.setLenient(false);

  try {

    // if not valid, it will throw ParseException
      Date date = sdf.parse(dateToValidate);
      System.out.println(date);

    } catch (ParseException e) {

      e.printStackTrace();
      return false;
    }

    return true;
  }
}